<template>
<div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="comment.reply_count + '条回复'">
      <van-icon slot="left" name="cross" @click='$emit("showOff")'/>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论" >
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        src="https://img.yzcdn.cn/vant/cat.jpeg"
      />
      <span style="color: #466b9d;" slot="title">{{ comment.aut_name }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ comment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{ comment.pubdate | relativeTime }}</span>
          <van-button
            size="mini"
            type="default"
          >回复 {{ comment.reply_count }}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
<!-- 对当前评论 回复列表 -->
<van-list
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onload"
  >
  <van-cell
    v-for="(item, index) in list"
    :key="index"
  >
    <van-image
      slot="icon"
      round
      width="30"
      height="30"
      style="margin-right: 10px;"
      :src="item.aut_photo"
    />
    <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
    <div slot="label">
      <p style="color: #363636;">{{item.content}}</p>
      <p>
        <span style="margin-right: 10px;">{{item.pubdate | relativeTime }}</span>
      </p>
    </div>
  </van-cell>
</van-list>
<!-- 回复列表 -->
<!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model.trim="value"
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="addComment"
      >
        <van-button slot="button" size="mini" type="info" @click="addComment"
          >发布</van-button
        >
      </van-field>
    </van-cell-group>
    <!-- /发布评论 -->
</div>
</template>
<script>

import { reqGetComment, reqAddCommentReply } from '@/api/comment'// reqAddCommentReply

export default ({
  props: {
    comment: {
      type: Object,
      required: true
    },
    artId: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      offset: null,
      value: '',
      id: ''
    }
  },
  methods: {
    async onload () {
      // eslint-disable-next-line camelcase
      // eslint-disable-next-line camelcase
      const { data: { data: { results, last_id } } } = await reqGetComment('c', this.comment.com_id.toString(), this.offset)
      console.log(results)
      if (results.length) {
        this.id = results[0].aut_id
      }
      // eslint-disable-next-line camelcase
      this.offset = last_id
      this.list.push(...results)
      this.loading = false
      if (!results.length) {
        this.finished = true
      }
    },
    async  addComment () {
      if (!this.id) return this.$toast.fail('读者禁止评论')
      if (!this.value) return
      try {
        // eslint-disable-next-line camelcase
        const {
          data: {
            // eslint-disable-next-line camelcase
            data: { new_obj }
          }
        } = await reqAddCommentReply(this.id, this.value, this.artId)
        this.list.unshift(new_obj)
        this.$toast.fail('文章评论成功')
        this.value = ''
      } catch (error) {
        this.$toast.fail('文章禁止评论')
      }
    }
  }
})
</script>

<style scoped lang='scss'>
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
